package com.masterclient.app.modules.coupon.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.common.bean.base.CommonBaseBean;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.coupon.beans.NewCouponBean;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
* Created by TMVPHelper on 2017/08/04
*/
public class CouponPresenter extends CouponContract.Presenter{

    @Override
    public void getCouponList(String type, String page, String pageSize) {
        Observable<NewCouponBean> observable = RetrofitUtil2.getInstance().getCouponList(type,page,pageSize);
        Disposable disposable = toSubscribe(observable, (Consumer<NewCouponBean>) bean -> {
            if (bean != null && "0".equals(bean.getCode())) {
                mView.onCouponListSuccess(bean);
            } else {
                mView.loadFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.loadFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getReceiveCouponList(String page, String pageSize) {
        Observable<NewCouponBean> observable = RetrofitUtil2.getInstance().getReceiveCouponList(page,pageSize);
        Disposable disposable = toSubscribe(observable, (Consumer<NewCouponBean>) bean -> {
            if (bean != null && "0".equals(bean.getCode())) {
                mView.onReceiveCouponListSuccess(bean);
            } else {
                mView.loadFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.loadFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void pickUpSalesrule(String coupon_code,String parent_product_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("coupon_code",coupon_code);
            jsonObject.put("parent_product_id",parent_product_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CommonBaseBean> observable = RetrofitUtil2.getInstance().coupon_receive(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if ("0".equals(bean.getCode())){
                    mView.pickUpSalesruleSuccess(coupon_code,bean.getMsg());
                }else {
                    mView.pickUpSalesruleFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.pickUpSalesruleFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

}